package com.xmh.ffmpeg;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.jupiter.api.Test;

import java.io.*;

import java.util.UUID;

public class ReadImg {

    /**
     * @param
     * @throws Exception
     */
    @Test
    public void test2() throws Exception {
//        String url = "https://softforspeed.51xiazai.cn/down/QQ_PCDownload1100109234.exe";
//        String url = "https://e1v-h.phncdn.com/hls/videos/202004/20/305761591/,1080P_4000K,720P_4000K,480P_2000K,240P_400K,_305761591.mp4.urlset/seg-4-f2-v1-a1.ts?validfrom=1620048791&validto=1620055991&ip=38.143.3.108&ipa=38.143.3.108&hdl=-1&hash=vhxag9NFkMyTr2P4ca%2FY9JlpC40%3D";
        String filePath = "K:\\word_IDEA\\ffmpeg\\Download\\";
        for (int i = 16; i < 17; i++) {
            String videoName = "index" + i;

//            String url = "thunder://QUFodHRwczovL3d3dy5tbXh6eGwxLmNvbS9jb21tb24vL21hb21pLy93bV9CY2FDVVZoci93bV9CY2FDVVZoci5tcDRaWg==" + videoName + ".ts";
            String url = "https://e1v-h.phncdn.com/hls/videos/202001/13/276409191/,720P_4000K,480P_2000K,240P_400K,_276409191.mp4.urlset/seg-149-f1-v1-a1.ts?validfrom=1620960906&validto=1620968106&ip=38.143.3.108&ipa=38.143.3.108&hdl=-1&hash=buGaCr%2FA%2BnJJbTiQFuY7L%2FL3724%3D";

            String uuid = videoName + ".jpg";

            downPictor(url, uuid);
//            DownloadVideo downloadVideo = new DownloadVideo();
//            downloadVideo.downloadNet(url, filePath);

            Thread.sleep(50);
        }
    }


    public static void downPictor(String url, String name) {
        CloseableHttpClient httpClient = null;
        InputStream inputStream = null;
        File file = null;
        try {
            httpClient = HttpClients.createDefault();
            HttpGet method = new HttpGet(url);

            addHeader(method);

            HttpResponse result = httpClient.execute(method);
            inputStream = result.getEntity().getContent();
            //得到图片的二进制数据，以二进制封装得到数据，具有通用性
            byte[] data = readInputStream(inputStream);
            //new一个文件对象用来保存图片，默认保存当前工程根目录
            File imageFile = new File("K:\\word_IDEA\\ffmpeg\\Download\\" + name);
            //创建输出流
            FileOutputStream outStream = new FileOutputStream(imageFile);
            //写入数据
            outStream.write(data);
            //关闭输出流
            outStream.close();
        } catch (Exception e) {
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {

            }
            try {
                httpClient.close();
            } catch (IOException e) {

            }
        }
    }


    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        //每次读取的字符串长度，如果为-1，代表全部读取完毕
        int len = 0;
        //使用一个输入流从buffer里把数据读取出来
        while ((len = inStream.read(buffer)) != -1) {
            //用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        //关闭输入流
        inStream.close();
        //把outStream里的数据写入内存
        return outStream.toByteArray();
    }

    private static void addHeader(HttpGet method) {
        String headerStr = ":authority: e1v-h.phncdn.com\n" +
                ":method: GET\n" +
                ":path: /hls/videos/202004/20/305761591/,1080P_4000K,720P_4000K,480P_2000K,240P_400K,_305761591.mp4.urlset/seg-4-f2-v1-a1.ts?validfrom=1620048791&validto=1620055991&ip=38.143.3.108&ipa=38.143.3.108&hdl=-1&hash=vhxag9NFkMyTr2P4ca%2FY9JlpC40%3D\n" +
                ":scheme: https\n" +
                "accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\n" +
                "accept-encoding: gzip, deflate, br\n" +
                "accept-language: zh-CN,zh;q=0.9\n" +
                "cache-control: no-cache\n" +
                "pragma: no-cache\n" +
                "sec-ch-ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"90\", \"Google Chrome\";v=\"90\"\n" +
                "sec-ch-ua-mobile: ?0\n" +
                "sec-fetch-dest: document\n" +
                "sec-fetch-mode: navigate\n" +
                "sec-fetch-site: none\n" +
                "sec-fetch-user: ?1\n" +
                "upgrade-insecure-requests: 1\n" +
                "user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36";
        String[] headerList = headerStr.split("\n");
        for (String header : headerList) {
            String[] headerKeyValue = header.split(": ");
            method.addHeader(headerKeyValue[0], headerKeyValue[1]);
        }
    }

}
